package com.qf.petmall.starter.localstores.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.petmall.starter.localstores.entity.Localstores;
import com.qf.petmall.starter.localstores.entity.Commodity;
import com.qf.petmall.starter.localstores.mapper.LocalstoresMapper;
import com.qf.petmall.starter.localstores.service.ICommodityService;
import com.qf.petmall.starter.localstores.service.ILocalstoresService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Zhong_
 * @since 2022-12-06
 */
@Service
public class LocalstoresServiceImpl extends ServiceImpl<LocalstoresMapper, Localstores> implements ILocalstoresService {


    @Autowired
    ICommodityService commodityService;

    @Override
    public List<Localstores> getPage(Integer pageNum, Integer limit) {

        Page<Localstores> localstoresPage = new Page<>(pageNum, limit);

        this.page(localstoresPage);

        List<Localstores> records = localstoresPage.getRecords();

        List<Commodity> list = commodityService.list();

        for (Localstores localstores : records) {

            ArrayList<Commodity> commodities = new ArrayList<>();

            for (Commodity commodity : list) {

                if (localstores.getId() == commodity.getParentId()) {
                    commodities.add(commodity);
                }

            }
            localstores.setCommodity(commodities);
        }

        return records;
    }
}
